from django.contrib.auth.models import User, Group
from rest_framework import serializers
from appapi.models import Dog


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')


class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ('url', 'name')


class DogSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    desc = serializers.CharField(required=False, allow_blank=True, max_length=256)
    facade = serializers.CharField(required=False, allow_blank=True, max_length=256)
    Background = serializers.CharField(required=False, allow_blank=True, max_length=16)
    status = serializers.CharField(required=False, allow_blank=True, max_length=5)
    generation = serializers.IntegerField(required=False)
    weight = serializers.FloatField(required=False)  # decimal_places=7,
    grow = serializers.FloatField(required=False)
    growthan = serializers.FloatField(required=False)
    bearval = serializers.FloatField(required=False)
    bearnum = serializers.IntegerField(required=False)
    digoldval = serializers.FloatField(required=False)
    dignum = serializers.IntegerField(required=False)
    digsum = serializers.FloatField(required=False)  # decimal_places=7,
    created = serializers.DateTimeField(required=False)

    def create(self, validated_data):
        """
        Create and return a new `Snippet` instance, given the validated data.
        """
        return Dog.objects.create(**validated_data)

    def update(self, instance, validated_data):
        """
        Update and return an existing `Snippet` instance, given the validated data.
        """
        instance.desc = validated_data.get('desc', instance.desc)
        instance.weight = validated_data.get('weight', instance.weight)
        instance.grow = validated_data.get('grow', instance.grow)
        instance.growthan = validated_data.get('growthan', instance.growthan)
        instance.bearval = validated_data.get('bearval', instance.bearval)
        instance.bearnum = validated_data.get('bearnum', instance.bearnum)
        instance.bearval = validated_data.get('bearval', instance.bearval)
        instance.digoldval = validated_data.get('digoldval', instance.digoldval)
        instance.dignum = validated_data.get('dignum', instance.dignum)
        instance.digsum = validated_data.get('digsum', instance.digsum)
        instance.save()
        return instance